package com.matrix.draft.algorithm.util;

import com.matrix.draft.algorithm.product.LinkNode;

public class AlgoUtil {

    /**
     * new一个链表
     * @param n 元素个数
     * @return
     */
    public static LinkNode makeALink(int n){
        LinkNode head = new LinkNode();
        LinkNode p = new LinkNode(1);
        head.next = p;
        // 开始开辟
        for (int i = 2; i <= n; i++) {
            p = p.next = new LinkNode(i);
        }
        return head;
    }

    /**
     * 输出链表
     * @param node
     */
    public static void showLink(LinkNode node) {
        if (null == node) {
            System.out.println();
            return;
        }
        System.out.print(node.value + (node.next != null ? "->" : ""));
        showLink(node.next);
    }

    public static void showLink(LinkNode node,boolean isHead){
        if(isHead){
            showLink(node.next);
        }else{
            showLink(node);
        }
    }

}
